home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1996 / MacHack 1996.toast / Hacks / Hacks ’95 / Venus / myenv.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-23  |  6.1 KB  |  224 lines  |  [TEXT/KAHL]

  1. /*
  2.  ************************************************************************
  3.  *            Service C++ functions 
  4.  *         that support the standard environment for me
  5.  */
  6.  
  7. #pragma implementation
  8.  
  9. #include "myenv.h"
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <stdarg.h>
  14. #include <Sound.h>
  15.  
  16. /*
  17.  *-----------------------------------------------------------------------
  18.  *        Some global constant pertaining to input/output
  19.  */
  20.  
  21. const char _Minuses [] = "\
  22. -------------------------------------------------------------------------------";
  23.  
  24. const char _Asteriscs [] = "\
  25. *******************************************************************************";
  26.  
  27. const char _Equals [] = "\
  28. ===============================================================================";
  29.  
  30.  
  31. static const int System_Alert_rsc = -16411;            // ID of the System ALRT 
  32.                                                     // resource (in the System file)
  33. /*
  34.  *------------------------------------------------------------------------
  35.  *            Print an error message at stderr and abort
  36.  * Synopsis
  37.  *    volatile void _error(const char * message,... );
  38.  *    Message may contain format control sequences %x. Items to print 
  39.  *    with the control sequences are to be passed as additional arguments to
  40.  *    the function call.
  41.  */
  42.  
  43. static volatile void (*abort_function)(void) = (volatile void (*)())abort;
  44.  
  45. volatile void _error(const char * message,...)
  46. {
  47.   va_list args;
  48.   char buffer[1000];
  49.   va_start(args,message);            // Init 'args' to the beginning of
  50.                                     // the variable length list of args
  51.   vsprintf(buffer,message,args);
  52.   strncat(buffer,"\rSorry man, enough is enough, I'm outta here",sizeof(buffer)-strlen(buffer)-1);
  53.   CtoPstr(buffer);                            // Convert to Pascal-like string
  54.   ParamText((ConstStr255Param)buffer,nil,nil,nil); // Set the string in the Alert resource
  55.   Alert(System_Alert_rsc,0L);                // Display the Alert box and the string
  56.                                               // set just before
  57.   abort_function();                            // And crash it, at last
  58. }
  59.  
  60. void set_abort_function(volatile void (*custom_abort)(void))
  61. {
  62.     if( custom_abort == nil )
  63.       abort_function = (volatile void (*)())abort;
  64.     else
  65.       abort_function = custom_abort;
  66. }
  67.  
  68.                                     // This guy handles the situation when the memory
  69.                                     // allocation through 'new' has failed
  70. void my_new_failed_handler(void)
  71. {
  72.     _error("Flat out of memory");
  73. }
  74.  
  75. /*
  76.  *------------------------------------------------------------------------
  77.  *                    Print a message at stderr
  78.  * Synopsis
  79.  *    void message(const char * text,... );
  80.  *    Message may contain format control sequences %x. Items to print 
  81.  *    with the control sequences are to be passed as additional arguments to
  82.  *    the function call.
  83.  */
  84.  
  85. void message(const char * text,...)
  86. {
  87.   va_list args;
  88.   char buffer[1000];
  89.   va_start(args,text);                        // Init 'args' to the beginning of
  90.                                             // the variable length list of args
  91.   vsprintf(buffer,text,args);
  92.   CtoPstr(buffer);                            // Convert to Pascal-like string
  93.   ParamText((ConstStr255Param)buffer,nil,nil,nil); // Set the string in the Alert resource
  94.   Alert(System_Alert_rsc,0L);                // Display the Alert box and the string
  95. }
  96.  
  97. /*
  98.  *------------------------------------------------------------------------
  99.  *                            Some more advanced error handling
  100.  */
  101.  
  102. ErrHandler::ErrHandler(void)
  103. {
  104.   raise_hell = TRUE;
  105.   was_error = FALSE;
  106. }
  107.  
  108.                         // Be lenient in case of error
  109. void ErrHandler::lenient(void)
  110. {
  111.   if( was_error )
  112.     _error("You've got an error you haven't handled!");
  113.   raise_hell = FALSE;
  114. }
  115.  
  116.                         // Have we failed?
  117. Boolean ErrHandler::was_ok(void)
  118. {
  119.   raise_hell = TRUE;
  120.   if( was_error )
  121.   {
  122.     was_error = FALSE;
  123.     return FALSE;
  124.   }
  125.   return TRUE;
  126. }
  127.  
  128.                         // Notify the user about the error and raise the hell if needed
  129. void ErrHandler::error(const char * message,...)
  130. {
  131.   va_list args;
  132.   char buffer[1000];
  133.   va_start(args,message);            // Init 'args' to the beginning of
  134.                                     // the variable length list of args
  135.   vsprintf(buffer,message,args);
  136.   if( raise_hell )
  137.       strncat(buffer,"\rRaising the hell - crashing",sizeof(buffer)-strlen(buffer)-1);
  138.   CtoPstr(buffer);                            // Convert to Pascal-like string
  139.   ParamText((ConstStr255Param)buffer,nil,nil,nil); // Set the string in the Alert resource
  140.   Alert(System_Alert_rsc,0L);                // Display the Alert box and the string
  141.                                               // set just before
  142.   if( raise_hell )
  143.     abort_function();                        // And crash it, at last
  144.   else
  145.   {
  146.     raise_hell = TRUE;
  147.     was_error = FALSE;
  148.   }                                            // And let it go for now
  149. }
  150.  
  151. //------------------------------------------------------------------------
  152. //                            Sound playing on errors
  153.  
  154.  
  155. static Handle error_sound_handle = nil;
  156.  
  157. #if 0
  158. static pascal void error_sound_player(short sound_no)
  159. {
  160.   if( error_sound_handle != nil )
  161.     SndPlay(nil,error_sound_handle,FALSE);
  162.   else
  163.     SysBeep(10);
  164.   error_sound_handle = nil;
  165. }
  166. #endif
  167.  
  168. void set_error_sound(Str255 sound_name)
  169. {
  170. #if 0
  171.   error_sound_handle = GetNamedResource('snd ',sound_name);
  172.   if( error_sound_handle != nil )
  173.     ErrorSound((pascal void (*)())error_sound_player);
  174. #endif
  175. }
  176.  
  177.  
  178. /*
  179.  *------------------------------------------------------------------------
  180.  *               Suspending the application for a specified period of time
  181.  *
  182.  * Unlike Delay(), the present function is generous in that in relinquishes the
  183.  * CPU control for a specified time and thus lets other (backgrounded) application
  184.  * run.
  185.  * The generous waiting is implemented as waiting for a null event which is to
  186.  * be sent to the application after the specifed timed interval expired. Note
  187.  * the application can get woken up before the time comes if other application
  188.  * calls WakeUpProcess().
  189.  */
  190.  
  191. void sleep(const int nticks)
  192. {
  193.     EventRecord event;
  194.     const int event_mask = 0;                // Wait for null events only
  195.     WaitNextEvent(event_mask,&event,nticks,nil);
  196. }
  197.  
  198. /*
  199.  *------------------------------------------------------------------------
  200.  *                            Initialize the Macintosh
  201.  *                 Initialize all the managers & memory
  202.  */
  203.  
  204. void Initialize_MAC(void)
  205. {
  206.     MaxApplZone();
  207.     
  208.     InitGraf(&qd.thePort);
  209.     InitFonts();
  210.     FlushEvents(everyEvent, 0);
  211.     InitWindows();
  212.     InitMenus();
  213.     TEInit();
  214.     InitDialogs(0L);
  215.     InitCursor();
  216.     
  217. #if 0
  218.     extern void (*_new_handler)(void);
  219.     _new_handler = my_new_failed_handler;
  220. #endif
  221. }
  222.  
  223.  
  224.